package com.symaster.common.swing.element;

import javax.swing.*;
import java.awt.*;

/**
 * @author yinmiao
 * @date 2021-09-22 10:55
 */
public class ImagePanel extends JComponent {

    public static final int SHOW_TYPE_CENTER = 1;
    public static final int SHOW_TYPE_LEFT_TOP = 0;

    private final int showType;
    private final Integer imageWidth;
    private final Integer imageHeight;
    private final Image image;

    public ImagePanel(Image image, int showType) {
        this.image = image;
        this.imageWidth = image.getWidth(this);
        this.imageHeight = image.getHeight(this);
        this.showType = showType;
        this.setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        int x, y;
        switch (showType) {
            case SHOW_TYPE_CENTER:
                int width = getWidth();
                int height = getHeight();
                x = (width / 2) - (imageWidth / 2);
                y = (height / 2) - (imageHeight / 2);
                break;
            case SHOW_TYPE_LEFT_TOP:
                x = 0;
                y = 0;
                break;
            default:
                x = 0;
                y = 0;
                break;
        }
        g.drawImage(image, x, y, this);
    }
}
